﻿using Microsoft.EntityFrameworkCore;
using System.Reflection;
using WAU.Models.AppUserModels;
using WAU.Models.BenchmarkModels;
using WAU.Models.CommodityModels;
using WAU.Models.Framework;
using WAU.Models.OrderModels;
using WAU.Models.OrderModels.OrderDeleteModels;
using WAU.Models.UserDailyActivityModels;

namespace WAU.DAL
{
    public class EssentialDbContext : DbContext
    {
        public EssentialDbContext(DbContextOptions<EssentialDbContext> options)
            : base(options)
        {
            this.ChangeTracker.AutoDetectChangesEnabled = false;
        }

        #region 用户、角色、权限 相关

        /// <summary>
        /// 用户
        /// </summary>
        public DbSet<AppUser> AppUser { get; set; }

        /// <summary>
        /// 角色
        /// </summary>
        public DbSet<AppRole> AppRole { get; set; }

        /// <summary>
        /// 用户角色关系
        /// </summary>
        public DbSet<AppUserRoleREL> AppUserRoleREL { get; set; }

        /// <summary>
        /// 用户层级
        /// </summary>
        public DbSet<AppUserHierarchy> AppUserHierarchy { get; set; }

        /// <summary>
        /// 菜单
        /// </summary>
        public DbSet<AppMenu> AppMenu { get; set; }

        /// <summary>
        /// 权限
        /// </summary>
        public DbSet<AppRoleMenuREL> AppRoleMenuREL { get; set; }



        #region Delete

        /// <summary>
        /// 删除的用户
        /// </summary>
        public DbSet<Z_DeleteAppUser> Z_DeleteAppUser { get; set; }

        /// <summary>
        /// 删除的角色
        /// </summary>
        public DbSet<Z_DeleteAppRole> Z_DeleteAppRole { get; set; }

        /// <summary>
        /// 删除的用户角色关系
        /// </summary>
        public DbSet<Z_DeleteAppUserRoleREL> Z_DeleteAppUserRoleREL { get; set; }

        /// <summary>
        /// 删除的用户层级
        /// </summary>
        public DbSet<Z_DeleteAppUserHierarchy> Z_DeleteAppUserHierarchy { get; set; }

        /// <summary>
        /// 删除的菜单
        /// </summary>
        public DbSet<Z_DeleteAppMenu> Z_DeleteAppMenu { get; set; }

        /// <summary>
        /// 删除的权限
        /// </summary>
        public DbSet<Z_DeleteAppRoleMenuREL> Z_DeleteAppRoleMenuREL { get; set; }

        #endregion

        #endregion


        #region 商品相关

        /// <summary>
        /// 商品
        /// </summary>
        public DbSet<Commodity> Commodity { get; set; }

        /// <summary>
        /// 商品主图
        /// </summary>
        public DbSet<CommoditySlideShowImage> CommoditySlideShowImage { get; set; }

        /// <summary>
        /// 商品 SKU
        /// </summary>
        public DbSet<CommoditySKU> CommoditySKU { get; set; }

        /// <summary>
        /// 商品分类
        /// </summary>
        public DbSet<CommodityCategory> CommodityCategory { get; set; }


        #region Delete

        /// <summary>
        /// 删除的商品
        /// </summary>
        public DbSet<Z_DeleteCommodity> Z_DeleteCommodity { get; set; }

        /// <summary>
        /// 删除的商品主图
        /// </summary>
        public DbSet<Z_DeleteCommoditySlideShowImage> Z_DeleteCommoditySlideShowImage { get; set; }

        /// <summary>
        /// 删除的商品 SKU
        /// </summary>
        public DbSet<Z_DeleteCommoditySKU> Z_DeleteCommoditySKU { get; set; }

        #endregion

        #endregion

        #region 订单相关

        /// <summary>
        /// 订单
        /// </summary>
        public DbSet<Order> Order { get; set; }

        #region Delete

        /// <summary>
        /// 删除的订单
        /// </summary>
        public DbSet<Z_DeleteOrder> Z_DeleteOrder{ get; set; }

        #endregion

        #endregion

        #region 日常活动

        /// <summary>
        /// 日常签到
        /// </summary>
        public DbSet<UserDailySign> UserDailySign { get; set; }

        #endregion

        public DbSet<GuidTest> GuidTest { get; set; }


        /// <summary>
        /// 重写模型创建事件时调用的方法
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // 找到当前类中所有的 继承自 BaseModel 的 DbSet 泛型参数，为它们设置行版本（并发操作控制）
            PropertyInfo[] pis = this.GetType().GetProperties()
                                    .Where(t => t.PropertyType.IsGenericType
                                                && t.PropertyType.GetGenericTypeDefinition() == typeof(DbSet<>)
                                                && t.PropertyType.GenericTypeArguments[0].IsSubclassOf(typeof(BaseModel)))
                                    .ToArray();
            foreach (var pi in pis)
            {
                modelBuilder.Entity(pi.PropertyType.GenericTypeArguments[0], e =>
                {
                    e.HasKey("Id");
                    // TODO 将 ModifyTime 作为版本控制字段后，直接对 ModifyTime 赋值的操作会被 EF 拦截（TrackAll 模式下）
                    e.Property("ModifyTime").IsRowVersion().IsConcurrencyToken();
                });
            }
        }
    }
}
